import { HttpCacheService } from '@core/net/http-cache.service';
import { Component } from '@angular/core';
import { BROWSER, VERSION_VALUE, VERSION_KEY } from './store/dict';
import { NzModalService } from 'ng-zorro-antd/modal';

@Component({
	selector: 'app-root',
	template: '<router-outlet></router-outlet>'
})
export class AppComponent {
	title = 'app';
	constructor(
		private httpCacheService: HttpCacheService,
		private nzModalService: NzModalService,
	) {

	}

	init() {
		const browser = this.httpCacheService.getLocalCache(BROWSER);
		if (this.getBrowserType() !== 'Chrome' && !browser) {
			this.nzModalService.warning({
				nzTitle: '浏览器提醒',
				nzContent: '请使用Chrome浏览器获得最佳系统体验',
				nzOkText: '知道了',
				nzCancelText: '不再提醒',
				nzOnCancel: () => {
					this.httpCacheService.saveLocalCache(BROWSER, 'true');
				}
			});
		}
		if (this.getVersion()) {
			if (VERSION_VALUE !== this.getVersion()) {
				this.nzModalService.warning({
					nzTitle: '版本提醒',
					nzContent: '检测到NCR版本已更新,请刷新后使用',
					nzOkText: '知道了',
					nzCancelText: '不再提醒',
					nzOnCancel: () => {
						this.httpCacheService.saveLocalCache(VERSION_KEY, VERSION_VALUE);
					},
					nzOnOk: () => {
						this.httpCacheService.saveLocalCache(VERSION_KEY, VERSION_VALUE);

					}
				});
			}
		} else {
			this.httpCacheService.saveLocalCache(VERSION_KEY, VERSION_VALUE);
		}
	}
	getBrowserType() {
		const userAgent = navigator.userAgent;
		if (userAgent.indexOf('Opera') > -1 || userAgent.indexOf('OPR') > -1) {
			return 'Opera';
		} else if (userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1) {
			return 'IE';
		} else if (userAgent.indexOf('Edge') > -1) {
			return 'Edge';
		} else if (userAgent.indexOf('Firefox') > -1) {
			return 'Firefox';
		} else if (userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Chrome') === -1) {
			return 'Safari';
		} else if (userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Safari') > -1) {
			return 'Chrome';
		} else if (!!(<any>window).ActiveXObject || 'ActiveXObject' in window) {
			return 'IE>=11';
		} else {
			return 'Unkonwn';
		}
	}

	getVersion() {
		return this.httpCacheService.getLocalCache(VERSION_KEY);
	}
}
